﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Common;
using Jzy.Core.Utility;
using ECommerce.Model.Member;
using ECommerce.BLL.Member;
using Jzy.Core.Common.Web;
using Jzy.Common;
using Jzy.Model.Common;

namespace ECommerce.Web.ECAdmin.Member
{
    public partial class Edit : PageEdit
    {
       // protected string AreaCode;

        private MemberBaseBO Member
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrp();

                if (!string.IsNullOrEmpty(this.PKID))
                {
                    DispInfo();
                }
                else
                {
                    lblRem.Visible = false;
                    lblRemain.Visible = false;
                }
            }
            AddAttr();
        }

        private void AddAttr()
        {
            WebUtil.SetTimeBox(Birthday, false);
        }

        private void DispInfo()
        {
            MemberBaseInfo info = Member.GetInfo(this.PKID);
            EntityUtil.SetControl(pnlMain, info);

            txtPassword.Attributes.Add("value", info.Password);
            hidPassword.Value = info.Password;

            //this.AreaCode = info.AreaCode.ToString();

            this.drpGender.SelectedValue = info.Gender.ToString();
            if (info.Birthday != null)
            {
                this.Birthday.Text = info.Birthday.Value.ToString("yyyy-MM-dd");
            }

            lblRemain.Text = info.Remain.ToString();

        }

        private void BindDrp()
        {
            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(MemberState));
            State.DataSource = list;
            State.DataTextField = "Name";
            State.DataValueField = "ID";
            State.DataBind();

            list = ConvertUtil.ConvertEnum(typeof(MemberLevel));
            Level.DataSource = list;
            Level.DataTextField = "Name";
            Level.DataValueField = "ID";
            Level.DataBind();

            list = ConvertUtil.ConvertEnum(typeof(Gender));
            drpGender.DataSource = list;
            drpGender.DataTextField = "Name";
            drpGender.DataValueField = "ID";
            drpGender.DataBind();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strLoginName = LoginName.Text.Trim();
            bool blnFlag = Member.ExistField(TableField.LoginName,strLoginName, this.PKID);
            if (blnFlag)
            {
                JSUtil.Alert("该登录名已存在");
            }
            else
            {
                MemberBaseInfo info = this.Member.GetInfo(this.PKID);
                EntityUtil.SetEntity(pnlMain, info);
                info.Gender = int.Parse(drpGender.SelectedValue);
                //if (!string.IsNullOrEmpty(Request.Form["AreaCode"]))
                //{
                //    info.AreaCode = int.Parse(Request.Form["AreaCode"]);
                //}

                if (string.IsNullOrEmpty(this.PKID))
                {
                    info.Password = txtPassword.Text;
                    info.InputTime = DateTime.Now;
                }
                else
                {
                    if (txtPassword.Text.Trim() != hidPassword.Value)
                    {
                        info.Password = txtPassword.Text;
                        info.Password = this.Member.Encript(info);
                    }
                }
                this.Member.Save(info);
                JSUtil.RefreshView();
            }
        }
    }
}